package main.java.FSMDE.persistence;

import java.util.ArrayList;

import main.java.ERMDE.models.Diagram;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.InterfaceCollection;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class InterfaceCollectionXmlManager implements
		IXmlManager<InterfaceCollection> {

	private ArrayList<Diagram> ermDiagrams;

	public Element getElementFromItem(InterfaceCollection interfaceCollection,
			Document document) {
		Element element = document.createElement("interfaces");

		for (Interface inface : interfaceCollection) {
			element.appendChild(new InterfaceXmlManager().getElementFromItem(
					inface, document));
		}

		return element;
	}

	public InterfaceCollection getItemFromXmlElement(
			Element interfaceCollectionElement) throws Exception {
		if (ermDiagrams == null) {
			ermDiagrams = new ArrayList<Diagram>();
		}

		InterfaceCollection interfaceCollection = new InterfaceCollection();

		if (interfaceCollectionElement == null)
			return interfaceCollection;
		NodeList interfaces = interfaceCollectionElement
				.getElementsByTagName("interface");

		for (int i = 0; i < interfaces.getLength(); i++) {
			Element interfaceElement = (Element) interfaces.item(i);
			InterfaceXmlManager interfaceXmlManager = new InterfaceXmlManager();
			interfaceXmlManager.setErmDiagrams(ermDiagrams);
			interfaceCollection.add(interfaceXmlManager
					.getItemFromXmlElement(interfaceElement));
		}

		return interfaceCollection;
	}

	public void setErmDiagrams(ArrayList<main.java.ERMDE.models.Diagram> ermDiagrams) {
		this.ermDiagrams = ermDiagrams;
	}

}
